package com.taxi.driver.module.main.mine.message.details;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ScrollView;
import android.widget.TextView;

import com.qianxx.view.HeadView;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseFragment;
import com.taxi.driver.event.MsgEvent;
import com.taxi.driver.module.main.mine.message.details.dagger.DaggerMessageDetailsComponent;
import com.taxi.driver.module.main.mine.message.details.dagger.MessageDetailsModule;
import com.taxi.driver.module.vo.MessageVO;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageDetailsFragment extends BaseFragment implements MessageDetailsContract.View {

    @BindView(R.id.head_view)
    HeadView mHeadView;
    @BindView(R.id.sv)
    ScrollView mSvContent;
    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.web_view)
    WebView mWebView;

    @Inject
    MessageDetailsPresenter mPresenter;

    private MessageVO msg;

    public static MessageDetailsFragment newInstance(MessageVO msg) {
        Bundle args = new Bundle();
        args.putSerializable(MessageDetailsActivity.MESSAGE_DETAILS, msg);
        MessageDetailsFragment fragment = new MessageDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_message_details, container, false);
        ButterKnife.bind(this, mView);

        msg = (MessageVO) getArguments().getSerializable(MessageDetailsActivity.MESSAGE_DETAILS);
        if (msg != null) {
            mHeadView.setTitle(TextUtils.isEmpty(msg.title) ? "系统通知" : msg.title);
            if (msg.sendType == 1) {
                mSvContent.setVisibility(View.VISIBLE);
                mWebView.setVisibility(View.GONE);
                mTvContent.setText(TextUtils.isEmpty(msg.content) ? "" : msg.content);
            } else if (msg.sendType == 2) {
                mSvContent.setVisibility(View.GONE);
                mWebView.setVisibility(View.VISIBLE);
                mWebView.getSettings().setJavaScriptEnabled(true);
                mWebView.setWebViewClient(new ReWebViewClient());
                mWebView.loadUrl(msg.linkUrl);
            }
            mPresenter.updateMsg(msg.uuid, msg.status);
        }
        return mView;
    }

    private class ReWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            showLoadingView(true);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            hideLoadingView();
            super.onPageFinished(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 监听网址变化
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        DaggerMessageDetailsComponent.builder()
                .appComponent(Application.getAppComponent())
                .messageDetailsModule(new MessageDetailsModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }

    /**
     * 消息状态更新成功
     */
    @Override
    public void updateSuccess() {
        // 发送事件通知消息列表改变状态
        EventBus.getDefault().post(new MsgEvent(MsgEvent.REFRESH_STATUS, msg.uuid));
    }
}
